//
//  SSMineViewController.m
//  starspaceVR
//
//  Created by 曹端芹 on 17/9/12.
//  Copyright © 2017年 starspace. All rights reserved.
//

#import "SSMineViewController.h"
#import "SSMineViewCell.h"
#import "SSMineHeaderView.h"
#import "SSUserInfoManager.h"
#import "SSUserInfoModel.h"

#import "SSMineInfoViewController.h"
#import "SSSettingAvatarViewController.h"
#import "SSModifyPsdViewController.h"

@interface SSMineViewController () <UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) SSMineHeaderView *headerView;
@property (nonatomic,strong) NSArray *imageNameArray;
@property (nonatomic,strong) NSArray *titleArray;

@end

static NSString *const mineViewCellID = @"mineViewCellID";

@implementation SSMineViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self requestData];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"我的";
    
    [self setupTableView];
    
}

- (void) requestData {
    
    kWeakSelf(self, weakSelf);
    NSDictionary *param = @{@"loginName":@"18821262365",@"password":@"1234567"};
    [SSNetworkManager getWithURLString:kSSUserInfoAPI parameters:param success:^(NSDictionary *responseDict) {
        
        NSDictionary *dict = responseDict[@"data"];
        NSLog(@"%@",dict);
        SSUserInfoModel *userInfo = [SSUserInfoModel mj_objectWithKeyValues:dict];
        [[SSUserInfoManager sharedManager] resetUserInfoWithUserInfo:userInfo];
        weakSelf.headerView.userInfo = userInfo;
    } failure:^(NSError *error) {
       
        
    }];
   
}

- (void) setupTableView {
    
    self.tableView.tableHeaderView = self.headerView;
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.view);
    }];
    
}

#pragma mark - UITableViewDelegate && UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.imageNameArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SSMineViewCell *cell = [tableView dequeueReusableCellWithIdentifier:mineViewCellID forIndexPath:indexPath];
    cell.imageName = self.imageNameArray[indexPath.row];
    cell.title = self.titleArray[indexPath.row];
    return cell;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.row == 0) {
        SSMineInfoViewController *mineInfoVC = [[SSMineInfoViewController alloc] initWithNibName:@"SSMineInfoViewController" bundle:nil];
        [self.navigationController pushViewController:mineInfoVC animated:YES];
    }else if (indexPath.row == 1){
        SSSettingAvatarViewController *settingAvatarVC = [[SSSettingAvatarViewController alloc] initWithNibName:@"SSSettingAvatarViewController" bundle:nil];
        [self.navigationController pushViewController:settingAvatarVC animated:YES];
    
    }else if (indexPath.row == 2){
        SSModifyPsdViewController *modifyPsdVC = [[SSModifyPsdViewController alloc] initWithNibName:@"SSModifyPsdViewController" bundle:nil];
        [self.navigationController pushViewController:modifyPsdVC animated:YES];
    
    
    }

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSArray *)imageNameArray {
    if (!_imageNameArray) {
        _imageNameArray = @[@"mine_usercenter",@"mine_settingavator",@"mine_changepsd",@"mine_findpsd",@"mine_sign"];
    }
    return _imageNameArray;
}

- (NSArray *)titleArray {
    if (!_titleArray) {
        _titleArray = @[@"个人信息",@"设置头像",@"修改密码",@"找回密码",@"点击签到"];
    }
    return _titleArray;
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerClass:[SSMineViewCell class] forCellReuseIdentifier:mineViewCellID];
    }
    return _tableView;
}

- (SSMineHeaderView *)headerView {
    if (!_headerView) {
        _headerView =  [[SSMineHeaderView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 160)];
    }
    return _headerView;
}



@end
